/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.settlement.service.carpresettlement;

import java.util.List;

import com.jeesite.modules.custom.service.CodeRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.settlement.entity.carpresettlement.TcarPresettlement;
import com.jeesite.modules.settlement.dao.carpresettlement.TcarPresettlementDao;
import java.util.Map;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.service.ServiceException;
import com.jeesite.modules.bpm.utils.BpmUtils;
import com.jeesite.modules.settlement.entity.carpresettlement.TcarPresettlementDetail;
import com.jeesite.modules.settlement.dao.carpresettlement.TcarPresettlementDetailDao;

/**
 * 车辆预结算单Service
 * @author shengbin
 * @version 2021-04-07
 */
@Service
@Transactional(readOnly=true)
public class TcarPresettlementService extends CrudService<TcarPresettlementDao, TcarPresettlement> {
	
	@Autowired
	private TcarPresettlementDetailDao tcarPresettlementDetailDao;
	@Autowired
	private CodeRuleService codeRuleService;
	/**
	 * 获取单条数据
	 * @param tcarPresettlement
	 * @return
	 */
	@Override
	public TcarPresettlement get(TcarPresettlement tcarPresettlement) {
		TcarPresettlement entity = super.get(tcarPresettlement);
		if (entity != null){
			TcarPresettlementDetail tcarPresettlementDetail = new TcarPresettlementDetail(entity);
			tcarPresettlementDetail.setStatus(TcarPresettlementDetail.STATUS_NORMAL);
			entity.setTcarPresettlementDetailList(tcarPresettlementDetailDao.findList(tcarPresettlementDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param tcarPresettlement 查询条件
	 * @param tcarPresettlement.page 分页对象
	 * @return
	 */
	@Override
	public Page<TcarPresettlement> findPage(TcarPresettlement tcarPresettlement) {
		return super.findPage(tcarPresettlement);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param tcarPresettlement
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(TcarPresettlement tcarPresettlement) {
		if (tcarPresettlement.getIsNewRecord()) {
			tcarPresettlement.setCarPresettlementId(codeRuleService.getCode("CarPreSettlement"));
			tcarPresettlement.setStatus1("2");
			tcarPresettlement.setApproveStatus("2");
		}

		super.save(tcarPresettlement);
		if (tcarPresettlement.getIsNewRecord()) {
			codeRuleService.updateCurrentNumber("CarPreSettlement");
		}
		// 保存 TcarPresettlement子表
		for (TcarPresettlementDetail tcarPresettlementDetail : tcarPresettlement.getTcarPresettlementDetailList()) {
			if (!TcarPresettlementDetail.STATUS_DELETE.equals(tcarPresettlementDetail.getStatus())) {
				tcarPresettlementDetail.setCarPresettlementId(tcarPresettlement);
				if (tcarPresettlementDetail.getIsNewRecord()) {
					tcarPresettlementDetailDao.insert(tcarPresettlementDetail);
				} else {
					tcarPresettlementDetailDao.update(tcarPresettlementDetail);
				}
			} else {
				tcarPresettlementDetailDao.delete(tcarPresettlementDetail);
			}
		}

	}
	
	/**
	 * 更新状态
	 * @param tcarPresettlement
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(TcarPresettlement tcarPresettlement) {
		super.updateStatus(tcarPresettlement);
	}
	
	/**
	 * 删除数据
	 * @param tcarPresettlement
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(TcarPresettlement tcarPresettlement) {
		super.delete(tcarPresettlement);
		TcarPresettlementDetail tcarPresettlementDetail = new TcarPresettlementDetail();
		tcarPresettlementDetail.setCarPresettlementId(tcarPresettlement);
		tcarPresettlementDetailDao.deleteByEntity(tcarPresettlementDetail);
	}
	
}